package com.nl.controller.web.controller;

import com.github.pagehelper.PageInfo;
import com.nl.controller.common.model.Response;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.service.SmsRoleService;
import com.nl.controller.web.bean.SmsRule;
import com.nl.controller.web.bean.SmsUsers;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Map;

@RestController
@RequestMapping("role")
@Slf4j
public class SmsRoleController {
    @Autowired
    private SmsRoleService roleService;

    @ApiOperation(value = "查询角色信息", notes = "分页查询角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "limit", value = "每页数量", required = true, dataType = "String"),
    })
    @PostMapping("find")
    public Response findRole(
            @RequestParam(value = "page", required = true) int page,
            @RequestParam(value = "limit", required = true) int limit,
            @RequestParam Map<String, Object> paramMap
    ) {
        PageInfo<SmsRule> pageInfo = roleService.findRoel(page, limit);
        log.info("角色查询：{}", paramMap.toString());
        log.debug("角色查询统计返回数据：{}", pageInfo.getList().toString());
        return ResponseUtil.sucess(pageInfo.getList(), pageInfo.getTotal());
    }

    @ApiOperation(value = "删除角色信息", notes = "删除角色信息")
    @ApiImplicitParam(name = "roleId", value = "用户编号", required = true, dataType = "String")
    @PostMapping("remove")
    public int findRole(@RequestParam(value = "roleId", required = true) String roleId
    ) {
        int result = roleService.removeRole(roleId);
        log.info("角色删除：{}", roleId);
        log.debug("角色删除返回数据：{}", result);
        return result;
    }

    @ApiOperation(value = "添加角色信息", notes = "添加角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "duty", value = "等级", required = true, dataType = "String"),
            @ApiImplicitParam(name = "function", value = "权限", required = true, dataType = "String"),
            @ApiImplicitParam(name = "roleName", value = "名字", required = true, dataType = "String"),
    })
    @PostMapping("add")
    public int insertRole(HttpSession session,
                          @RequestParam(value = "roleName", required = true) String roleName,
                          @RequestParam(value = "duty", required = true) String duty,
                          @RequestParam(value = "functionIds", required = true) String functionIds,
                          @RequestParam Map<String, Object> paramMap) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String msisdn = user.getMsisdn();
        int result = roleService.addRole(roleName, functionIds, duty, msisdn);
        log.info("角色添加参数：{}", paramMap.toString());
        log.debug("角色添加返回数据：{}", result);
        return result;
    }


    @ApiOperation(value = "查询单个角色信息", notes = "查询单个角色信息")
    @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "String")
    @PostMapping("findByRoleId")
    public SmsRule findRoleBYRoleId(
            @RequestParam(value = "roleId", required = true) String roleId
    ) {
        SmsRule smsRule = roleService.findRoleBYRoleId(roleId);
        log.info("查询单个角色信息：{}", roleId);
        log.debug("查询单个角色添加返回数据：{}", smsRule.toString());
        return smsRule;
    }


    @ApiOperation(value = "修改角色信息", notes = "修改角色信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色编号", required = true, dataType = "String"),
            @ApiImplicitParam(name = "duty", value = "等级", required = true, dataType = "String"),
            @ApiImplicitParam(name = "function", value = "权限", required = true, dataType = "String"),
            @ApiImplicitParam(name = "roleName", value = "名字", required = true, dataType = "String"),
    })
    @PostMapping("modify")
    public int insertRole(HttpSession session,
                          @RequestParam(value = "roleName", required = true) String roleName,
                          @RequestParam(value = "roleId", required = true) String roleId,
                          @RequestParam(value = "duty", required = true) String duty,
                          @RequestParam(value = "functionIds", required = true) String functionIds,
                          @RequestParam Map<String, Object> paramMap) {
        SmsUsers user = (SmsUsers) session.getAttribute("user");
        String msisdn = user.getMsisdn();
        int result=roleService.modifyRole(roleId, roleName, functionIds, duty, msisdn);
        log.info("角色修改参数：{}", paramMap.toString());
        log.debug("角色修改返回数据：{}", result);
        return result;
    }

}
